import axios from 'axios';
import { createRouter, createWebHashHistory } from "vue-router";
import store from '../store'
const routes = [
    {
        path: '/',
        redirect: 'hngk'
    },
    {
        path: '/',
        redirect: 'hngk',
        component: () => import('../pages/Home'),
        children: [
            {
                path: '/hngk',
                name: 'hngk',
                component: () => import('../pages/hngk/Hngk')
            },
            {
                path: '/sjhj',
                name: 'sjhj',
                component: () => import('../pages/sjhj/Sjhj')
            },
            {
                path: '/sjzl',
                name: 'sjzl',
                component: () => import('../pages/sjzl/Sjzl')
            },
            {
                path: '/jcyj',
                name: 'jcyj',
                component: () => import('../pages/jcyj/Jcyj')
            },
            {
                path: '/yjzy',
                name: 'yjzy',
                component: () => import('../pages/yjzy/Yjzy')
            },
            {
                path: '/hfcj',
                name: 'hfcj',
                component: () => import('../pages/hfcj/Hfcj')
            },
            {
                path: '/fxfk',
                name: 'fxfk',
                component: () => import('../pages/fxfk/Fxfk')
            },
            {
                path: '/sjzx',
                name: 'sjzx',
                component: () => import('../pages/sjzx/Sjzx')
            },
            {
                path: '/yjzy',
                name: 'yjzy',
                component: () => import('../pages/yjzy/Yjzy')
            },
        ],
    },
    
    {
        path: '/login',
        name: 'login',
        component: () => import('../pages/Login')
    },
    {
        path: '/judge',
        name: 'judge',
        component: () => import('../pages/judge/Judge')
    }

]
const router = createRouter({
    history: createWebHashHistory(),
    routes: routes
})
const areaCut = (data) => {
    let arr = data.split('/').slice(1);
    let result = arr.filter((item)=>{
        return item !="";
    })
    return result;
}

router.beforeEach((to, from, next) => {
    // 单点登陆功能
    // 跳转judge（一个跳转过渡的页面） --> 请求可视化后台用户认证接口，即可完成单点登录
    // 
    if (to.path == "/judge") {
        axios.get(`${window.sjzxUrl}/api/jwtUser`).then((res) => {
            console.log({'res':res})
            console.log({'resData':res.data})
            let tuRegion = areaCut(res.data.tuRegion);
            console.log(res.data.tudNames)
            let region = '';
            let depart = '';
            let thDepart = res.data.tudNames;
            if(thDepart==="海南省"){
                depart = "海南省"
            }else{
                let thDepart1 = areaCut(res.data.tudNames);
                depart = thDepart1[thDepart1.length-1];
            }
            console.log(depart)
            
            if(tuRegion.length==1){
                region = tuRegion[0]
            }else{
                region = tuRegion[1]
            }
            localStorage.setItem("Authorization",res.data.tokenAuth)
            store.commit('changeUserName',res.data.tuNickname)
            store.commit('changeUserRegion',region)
            store.commit('changeUserDepart',depart)
            console.log("跳转")
            router.push(`${to.query.pages}`)//fxfk
        }).catch((err) => {
            // next({ path: '/login' })
        })
    } else {
        if (to.path !== "/login") {
            let auth = localStorage.Authorization;
            if (auth ) {
                next();
            } else {
                console.log("走登录页")
                localStorage.removeItem('Authorization')
                localStorage.removeItem('userName')
                localStorage.removeItem('userRegion')
                localStorage.removeItem('userDepart')
                next({ path: '/login' })
            }
        } else {
            console.log("登出操作")
            next();
        }
    }

})
export { router }